package com.library.servlet.student;

import com.library.service.student.BorrowService;
import com.library.servlet.BaseServlet;
import com.library.entity.Student;
import com.library.util.StudentContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/student/borrow/*")
public class BorrowServlet extends BaseServlet {
    private BorrowService borrowService = new BorrowService();

    // 借书
    protected void borrowBooks(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        String[] barCodes = req.getParameterValues("barCode");
        String resultMsg = borrowService.borrowBooks(sno, barCodes);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }

    // 还书
    protected void returnBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String serNum = req.getParameter("serNum");
        String resultMsg = borrowService.returnBook(serNum);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }

    // 展示未归还图书
    protected void showUnreturned(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        java.util.List<Object[]> unreturnedList = borrowService.getUnreturnedBooks(sno);
        req.setAttribute("unreturnedList", unreturnedList);
        req.getRequestDispatcher("/WEB-INF/student/showUnreturned.jsp").forward(req, resp);
    }
} 